// proxySubShopDetail.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

const app = getApp()
var util = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    name: '',
    phone: null,
    address: '',
    rate: null,
    refund:0,
    total: null,
    order: null,
    income: null,
    time: '',
    update: true,
    rateUpdate: false,
    disabledStyle: 'color:#b2b2b2;--field-input-text-color:#b2b2b2',
    actionShow: false,
    newid: '',
    newidError: "",

    surplusRate: '',
    surplusShopRate:'',

    refundOption: [
      { text: '5分钟内', value: 5 },
      { text: '10分钟内', value: 10 },
      { text: '30分钟内', value: 30 },
      { text: '关闭', value: 0 }
    ],
  },
  // 表单数值改变
  onChange: function (detail, event) {// detail 为当前输入的值
    var that = this;
    that.setData({
      [detail]: event.detail
    })
  },
  nameChange: function (event) {
    this.onChange('name', event)
  },
  telChange: function (event) {
    this.onChange('phone', event)
  },
  addChange: function (event) {
    this.onChange('address', event)
  },
  rateChange: function (event) {
    this.onChange('rate', event)
  },
  newidChange: function (event) {
    this.onChange('newid', event)
  },
  clearnewErr: function () {
    this.setData({
      newidError: ""
    })
  },
  rateLimit:function(event){
    var surplusShopRate = this.data.surplusShopRate
    if (event.detail.value > surplusShopRate){
      this.setData({
        rate: surplusShopRate
      })
    }
    if (event.detail.value < 0) {
      this.setData({
        rate: 0
      })
    }
  },
  refundChange: function (event) {
    var that = this;
    that.setData({
      refund: event.detail
    })
  },
  // 更改表单可修改状态
  toUpdate: function () {
    if (this.data.update == false) {
      this.shopInfo();
    }
    this.setData({
      update: !this.data.update
    })
  },
  // 保存
  save: function () {
    var that = this;
    var surplusShopRate = this.data.surplusShopRate
    if (that.data.rate > surplusShopRate) {
      that.setData({
        rate: surplusShopRate
      })
    }
    var data = {
      id:that.data.id,
      name:that.data.name,
      tel: that.data.phone,
      address: that.data.address,
      commission: that.data.rate,
      refund: that.data.refund,
      latitude: that.data.latitude,
      longitude: that.data.longitude
    };
    wx.showLoading({
      title: '加载中...',
    })
    app.post('agent/submitShopInfo', data).then((res) => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: "修改成功",
          icon: 'success',
          duration: 2000
        })
        that.setData({
          update: true
        })
        var pages = getCurrentPages();
        var beforePage = pages[pages.length - 2];
        beforePage.onPullDownRefresh();
      } else {
        wx.showToast({
          title: "提交失败",
          icon: 'loading',
          duration: 2000
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.hideLoading();
      wx.showToast({
        title: errMsg,
        icon: 'loading',
        duration: 2000
      })
    })
  },
  // 获取表单信息
  shopInfo:function(){
    var that = this;
    var data = { id : that.data.id}
    app.post('agent/shopInfo',data).then((res)=>{
      var surplusShopRate = that.data.surplusRate - res.data.engCommission - res.data.salesmanCommission
      that.setData({
        name: res.data.shopName,
        phone: res.data.telphone,
        address: res.data.address,
        rate: res.data.commission,
        total: res.data.count,
        order: res.data.orderCount,
        income: res.data.amount,
        time: res.data.created,
        surplusShopRate: surplusShopRate,
        engCommission: res.data.engCommission,
        salesmanCommission: res.data.salesmanCommission,
        refund:res.data.refund,
        latitude: res.data.latitude,
        longitude: res.data.longitude
      })
    }).catch((errMsg)=>{
      console.log(errMsg)
    })
  },
  // 删除替换店铺
  todelete: function (e) {
    this.setData({
      actionShow: true,
    })
  },
  actionClose: function () {
    this.setData({
      actionShow: false,
      newid: '',
      newidError: ''
    })
  },
  deleteConfirm: function () {
    var that = this;
    var data = { 
      oldid:that.data.id,
      newid: that.data.newid 
    };
    app.post('agent/deleteShop', data).then((res) => {
      wx.hideLoading();
      if (res.code == 200) {
        that.setData({
          actionShow: false,
        })
        var pages = getCurrentPages();
        var beforePage = pages[pages.length - 2];
        beforePage.onPullDownRefresh();
        wx.navigateBack({
          delta:1
        })
        
      } else {
        that.setData({
          newidError: res.message
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.hideLoading();
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  },
  // 获取详细地址
  getAddress: function () {
    var that = this;
    if(!that.data.update){
      wx.getLocation({
        type: 'gcj02', //返回可以用于wx.openLocation的经纬度
        success(res) {
          const latitude = res.latitude
          const longitude = res.longitude
          wx.chooseLocation({
            latitude,
            longitude,
            success: function (res) {
              that.setData({
                address: res.address,
                latitude: res.latitude,
                longitude: res.longitude
              })
            },
          })
        }, fail(res) {
          Toast.fail("请检查您的微信或小程序是否开启定位服务")
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    var rateUpdate = false;
    if (options.level) {
      rateUpdate = true
    }
    this.setData({
      id: id,
      surplusRate: app.globalData.agentRate,
      rateUpdate: rateUpdate
    })
    this.shopInfo();
  }
})